package logic

import (
	"context"
	"errors"
	"house_srv/models/mysql"

	"house_srv/house"
	"house_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseReleaseLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseReleaseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseReleaseLogic {
	return &HouseReleaseLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HouseReleaseLogic) HouseRelease(in *house.HouseReleaseRequest) (*house.HouseReleaseResponse, error) {
	// todo: add your logic here and delete this line
	user := mysql.User{}
	err := user.Create()
	if user.Status != 2 {
		return nil, err
	}

	h := mysql.House{
		UserId:        in.UserId,
		HouseCity:     in.HouseCity,
		Address:       in.Address,
		HouseBuild:    in.HouseBuild,
		HouseFloor:    in.HouseFloor,
		HouseArea:     in.HouseArea,
		HousePrice:    float64(in.HousePrice),
		HouseDescribe: in.HouseDescribe,
		HouseCover:    in.HouseCover,
	}
	err = h.HouseInfo()
	if err != nil {
		logx.Error("创建房源信息不存在")
		return nil, errors.New("创建房源信息不存在")
	}
	err = h.Create()
	if err != nil {
		logx.Error("创建房源信息错误")
		return nil, errors.New("创建房源信息错误")
	}

	return &house.HouseReleaseResponse{
		HouseId: int64(h.ID),
	}, nil
}
